using System;
using System.Collections.Generic;
using System.Text;

public abstract class SGBaseBackGroundDownloader
{
    public abstract void ForceDownload(string urlRelativePath, string saveRelaitvepath, long fileSize);
    public abstract void AddDownload(List<BackGroundDownloadParam> downloadParams);
    public abstract void StartDownload();
    public abstract void PauseDownload();
    public abstract void SetDownloadParam(int maxCount, float silentIntervalTime, int silentBatchSize);
    public abstract long GetDownloadedSize();
    public abstract long GetAllDownloadSize();
    
    protected byte[] ConvertParamsToBytes(List<BackGroundDownloadParam> paramsArray)
    {
        var byteDataWriter = new ByteDataWriter(paramsArray.Count);
        byteDataWriter.WriteInt(paramsArray.Count);
        foreach (var param in paramsArray)
        {
            byteDataWriter.WriteString(param.UrlRelativePath);
            byteDataWriter.WriteString(param.SaveRelativePath);
            byteDataWriter.WriteLong(param.FileSize);
        }
        return byteDataWriter.Data.ToArray();
    }
    
    private class ByteDataWriter
    {
        private List<byte> m_Data;

        public List<byte> Data => m_Data;
        
        public ByteDataWriter(int capacity)
        {
            m_Data = new List<byte>(capacity);
        }

        public void WriteString(string value)
        {
            WriteInt(value.Length);
            var b = Encoding.UTF8.GetBytes(value);
            m_Data.AddRange(b);
        }

        public void WriteInt(int value)
        {
            var b = BitConverter.GetBytes(value);
            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(b); // 转成小端
            }
            m_Data.AddRange(b);
        }

        public void WriteLong(long value)
        {
            var b = BitConverter.GetBytes(value);
            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(b); // 转成小端
            }
            m_Data.AddRange(b);
        }
    }
}